package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.ClassStudentSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.exception.TransactionException;
import com.nwpu.coursesupport.service.ClassStudentService;
import com.nwpu.coursesupport.service.param.ClassStudentServiceParam;
import com.nwpu.coursesupport.service.param.ClassStudentServiceParam.SearchViewParam;
import com.nwpu.coursesupport.service.param.IDListParam;
import com.nwpu.coursesupport.tool.JSONUtils;

@Controller
@RequestMapping("/cscontrol")
public class ClassStudentController extends AbstractController {

	@Autowired
	private ClassStudentService csService;

	@RequestMapping(value = "/ajaxCreate", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxCreate( String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		ClassStudentServiceParam.CreateParam p = JSONUtils.json2pojo(data, ClassStudentServiceParam.CreateParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		rst = this.csService.create(p);
		return rst;
	}

	//班级学生  非ajax
	@RequestMapping("/listView")
	public String listView(@RequestParam("id") Integer classId,Integer pageNo,HttpServletRequest request) {
		SearchViewParam p = new SearchViewParam();
		p.setClassId(classId);
		p.setPageNo(pageNo==null?1:pageNo);
		JSONResult rst = this.csService.listView(p);
		request.setAttribute("data", this.parseData(rst));
		return "/teapage/classStudent";
	}
	
	//班级学生 并分页
	@RequestMapping("/ajaxListView")
	@ResponseBody
	public JSONResult ajaxListView(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		SearchViewParam p = JSONUtils.json2pojo(data, SearchViewParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		rst = this.csService.listView(p);
		return rst;
	}
	
	//班级中的学生 
	@RequestMapping("/ajaxListData")
	@ResponseBody
	public JSONResult ajaxListDate(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		ClassStudentSearchCriteria p = JSONUtils.json2pojo(data, ClassStudentSearchCriteria.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		rst = this.csService.findStudentByParam(p);
		return rst;
	}

	//搜索学生  
	@RequestMapping("/searchListData")
	@ResponseBody 
	public JSONResult searchListData(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		ClassStudentSearchCriteria p = JSONUtils.json2pojo(data, ClassStudentSearchCriteria.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		rst = this.csService.searchListData(p);
		return rst;
	}

	@RequestMapping(value = "/ajaxBatchCreateSmallClass", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxBatchCreateSmallClass(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		try {
			ClassStudentServiceParam.BatchCreateParam p = JSONUtils.json2pojo(data,
					ClassStudentServiceParam.BatchCreateParam.class);
			if (p == null) {
				return rst.fail("参数有误");
			}
			rst = csService.batchCreateSmallClass(p);
		} catch (TransactionException e) {// 如果出现失败，会抛异常，同时会回滚事物（因为添加）
			logger.error("抛出异常，批量添加学生失败");
			rst.fail("批量添加学生失败");
		}
		return rst;
	}
	
	
	@RequestMapping(value = "/ajaxBatchCreateClass", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxBatchCreateClass(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		try {
			ClassStudentServiceParam.BatchCreateClassParam p = JSONUtils.json2pojo(data,
					ClassStudentServiceParam.BatchCreateClassParam.class);
			if (p == null) {
				return rst.fail("参数有误");
			}
			rst = csService.batchCreateClass(p);
		} catch (TransactionException e) {// 如果出现失败，会抛异常，同时会回滚事物（因为添加）
			logger.error("抛出异常，批量添加学生失败");
			rst.fail("批量添加学生失败");
		}
		return rst;
	}
	
	@RequestMapping(value = "/parseExcel", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult parseExcel(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		try {
			ClassStudentServiceParam.CreateByExcelParam p = JSONUtils.json2pojo(data,
					ClassStudentServiceParam.CreateByExcelParam.class);
			if (p == null) {
				return rst.fail("参数有误");
			}
			rst = csService.parseByExcel(p);
		} catch (Exception e) {// 如果出现失败，会抛异常，同时会回滚事物（因为添加）
			rst.fail("后台出现异常");
		}
		return rst;
	}
	
	@RequestMapping(value = "/ajaxUpdate", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxUpdate(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		ClassStudentServiceParam.UpdateParam p = JSONUtils.json2pojo(data, ClassStudentServiceParam.UpdateParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		rst = this.csService.update(p);
		return rst;
	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@ResponseBody
	public JSONResult batchDelete(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		IDListParam p = JSONUtils.json2pojo(data, IDListParam.class);
		if (p == null) {
			return rst.fail("参数转换失败");
		}
		rst = this.csService.removeAll(p);
		return rst;
	}

}
